package dao.impl;

import dao.RatifyLessonDao;
import entity.RatifyLesson;
import util.JDBCUtils;

import java.sql.*;
import java.util.ArrayList;

public class RatifyLessonDaoImpl implements RatifyLessonDao {
    @Override
    public int approve(String code,String student,String lessoncode,String ifratify) throws Exception {

        int ifratifyint = Integer.parseInt(ifratify);
        if (ifratifyint==0||ifratifyint==2){ifratifyint=1;}else if(ifratifyint==1){ifratifyint=2;}

//            获取链接
        Connection conn = JDBCUtils.getconnection();
//            获取数据库操作对象
        Statement stat = conn.createStatement();
        try {
//            执行SQL语句
            String str = "UPDATE ratifylesson SET ifratify = " + ifratifyint + " WHERE code = " + code + " and student = " + "'" + student + "'" + " and lessoncode = " + lessoncode;
            System.out.println(str);
            int count = stat.executeUpdate(str);
            System.out.println(count == 1 ?"插入成功":"插入失败");
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
//            释放资源
            JDBCUtils.release(null,stat,conn);
        }
        return 0;
    }

    @Override
    public ArrayList<RatifyLesson> findAll() throws Exception {
        ArrayList<RatifyLesson> list = new ArrayList<>();
//            获取链接
        Connection conn = JDBCUtils.getconnection();
//            获取数据库操作对象
        Statement stat = conn.createStatement();

        ResultSet rs = null;
        try{
            //执行sql语句，并且接收返回的结果集
            String sql = "SELECT * FROM ratifylesson";
            rs = stat.executeQuery(sql);

            //处理结果集
            while(rs.next()) {
                String code = rs.getString("code");
                String lessoncode = rs.getString("lessoncode");
                String student = rs.getString("student");
                String lessonname = rs.getString("lessonname");
                String teacher = rs.getString("teacher");
                Integer ifratify = rs.getInt("ifratify");
                String disapprovereason = null;
                if(rs.getString("disapprovereason")!=null){
                    disapprovereason = rs.getString("disapprovereason");
                }
                String applyreason = null;
                if(rs.getString("applyreason")!=null){
                    applyreason = rs.getString("applyreason");
                }
                //封装RatifyLesson对象
                RatifyLesson ratifylesson= new RatifyLesson(code,lessoncode,student,lessonname,teacher,ifratify,disapprovereason,applyreason);

                //将RatifyLesson对象保存到集合中
                list.add(ratifylesson);
            }

        } catch(Exception e) {
            e.printStackTrace();
        } finally {
            //释放资源
            JDBCUtils.release(rs,stat,conn);
        }
        //将集合对象返回
        return list;
    }

    @Override
    public ArrayList<RatifyLesson> findByTeacher(String teachername) throws Exception {
        ArrayList<RatifyLesson> list = new ArrayList<>();
//            获取链接
        Connection conn = JDBCUtils.getconnection();
//            获取数据库操作对象
        Statement stat = conn.createStatement();

        ResultSet rs = null;
        try{
            //执行sql语句，并且接收返回的结果集
            String sql = "SELECT * FROM ratifylesson where teacher = " + "'" + teachername + "'";
            rs = stat.executeQuery(sql);

            //处理结果集
            while(rs.next()) {
                String code = rs.getString("code");
                String lessoncode = rs.getString("lessoncode");
                String student = rs.getString("student");
                String lessonname = rs.getString("lessonname");
                String teacher = rs.getString("teacher");
                Integer ifratify = rs.getInt("ifratify");
                String disapprovereason = null;
                if(rs.getString("disapprovereason")!=null){
                    disapprovereason = rs.getString("disapprovereason");
                }
                String applyreason = null;
                if(rs.getString("applyreason")!=null){
                    applyreason = rs.getString("applyreason");
                }
                //封装RatifyLesson对象
                RatifyLesson ratifylesson= new RatifyLesson(code,lessoncode,student,lessonname,teacher,ifratify,disapprovereason,applyreason);

                //将RatifyLesson对象保存到集合中
                list.add(ratifylesson);
            }

        } catch(Exception e) {
            e.printStackTrace();
        } finally {
            //释放资源
            JDBCUtils.release(rs,stat,conn);
        }
        //将集合对象返回
        return list;
    }

    @Override
    public void addordelete(RatifyLesson rlesson,String status) throws Exception {

        String code = rlesson.getCode();
        String lessoncode = rlesson.getLessoncode();
        String student = rlesson.getStudent();
        String lessonname = rlesson.getLessonname();
        String teacher = rlesson.getTeacher();
        int ifratify = rlesson.getIfratify();

        int statusint = Integer.parseInt(status);
//            获取链接
        Connection conn = JDBCUtils.getconnection();
//            获取数据库操作对象
        Statement stat = conn.createStatement();
        String str = null;
        try {
//            执行SQL语句
            if(statusint==0) {
                str = "INSERT INTO ratifylesson(code,lessoncode,student,lessonname,teacher,ifratify) VALUES (" + code +","+ lessoncode +",'"+ student +"','"+ lessonname +"','"+ teacher +"','"+ ifratify +"')";
            } else {
                str = "DELETE FROM ratifylesson WHERE lessoncode = " + lessoncode + " and student = " + "'" + student + "'";
            }
            System.out.println(str);
            int count = stat.executeUpdate(str);
            System.out.println(count == 1 ?"插入成功":"插入失败");
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            JDBCUtils.release(null,stat,conn);
        }
    }

    @Override
    public int disapprove(String lessoncode, String student, String disapprovereason) throws Exception {
//            获取链接
        Connection conn = JDBCUtils.getconnection();
//            获取数据库操作对象
        Statement stat = conn.createStatement();

        try {
//            执行SQL语句
            String str = "UPDATE ratifylesson SET disapprovereason = " + "'" + disapprovereason + "'" + " WHERE lessoncode = "+ lessoncode + " and student = " + "'" + student + "'";
            System.out.println(str);
            int count = stat.executeUpdate(str);
            System.out.println(count == 1 ?"插入成功":"插入失败");
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
//            释放资源
            JDBCUtils.release(null,stat,conn);
        }

        return 0;
    }

    @Override
    public int apply(String lessoncode, String student, String applyreason) throws Exception {
//            获取链接
        Connection conn = JDBCUtils.getconnection();
//            获取数据库操作对象
        Statement stat = conn.createStatement();

        try {
//            执行SQL语句
            String str = "UPDATE ratifylesson SET applyreason = " + "'" + applyreason + "'" + " WHERE lessoncode = "+ lessoncode + " and student = " + "'" + student + "'";
            System.out.println(str);
            int count = stat.executeUpdate(str);
            System.out.println(count == 1 ?"插入成功":"插入失败");
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
//            释放资源
            JDBCUtils.release(null,stat,conn);
        }

        return 0;
    }
}
